package com.base.common.core.aspect;

import com.base.common.core.annotation.LogRunTime;
import com.base.common.core.utils.AopUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Slf4j
@Aspect
public class LogRunTimeAspect {

    @Pointcut("@annotation(com.base.common.core.annotation.LogRunTime)")
    public void logRunTime(){}

    @Around(value = "logRunTime()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        LogRunTime annotation = AopUtil.getMethodAnnotationWithJoinPoint(proceedingJoinPoint, LogRunTime.class);
        String logName = proceedingJoinPoint.getSignature().getName();
        if (!"".equals(annotation.value())) {
            logName = annotation.value();
        }
        long start = System.currentTimeMillis();
        Object proceed = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
        log.info("{},耗时:{}秒", logName, (System.currentTimeMillis() - start) / 1000F);
        return proceed;
    }
}
